﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Gmantis.Controls
{
    [StyleTypedProperty(Property = "ItemStyle", StyleTargetType = typeof(ContentControl))]
    public class ItemsControl : System.Windows.Controls.ItemsControl
    {
        // Fields
        public static readonly DependencyProperty ItemStyleProperty = DependencyProperty.Register("ItemStyle", typeof(Style), typeof(ItemsControl), null);
        public new static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ItemsControl), null);
        public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(ItemsControl), null);

        // Methods
        public ItemsControl()
        {
            base.DefaultStyleKey = typeof(ItemsControl);
            this.SetCustomDefaultValues();
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            ContentControl control = new ContentControl();
            control.SetBinding(FrameworkElement.StyleProperty, new Binding().From<ItemsControl>(this, x => x.ItemStyle));
            return control;
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            if (!object.ReferenceEquals(element, item))
            {
                ContentControl control = element as ContentControl;
                DataTemplate template = (this.ItemTemplateSelector != null) ? this.ItemTemplateSelector.SelectTemplate(item, element) : this.ItemTemplate;
                control.ContentTemplate = template;
                control.Content = item;
            }
        }

        private void SetCustomDefaultValues()
        {
        }

        // Properties
        public Style ItemStyle
        {
            get
            {
                return (Style)base.GetValue(ItemStyleProperty);
            }
            set
            {
                base.SetValue(ItemStyleProperty, value);
            }
        }

        public new DataTemplate ItemTemplate
        {
            get
            {
                return (DataTemplate)base.GetValue(ItemTemplateProperty);
            }
            set
            {
                base.SetValue(ItemTemplateProperty, value);
            }
        }

        public DataTemplateSelector ItemTemplateSelector
        {
            get
            {
                return (DataTemplateSelector)base.GetValue(ItemTemplateSelectorProperty);
            }
            set
            {
                base.SetValue(ItemTemplateSelectorProperty, value);
            }
        }
    }

    public class DataTemplateSelector
    {
        public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            return null;
        }

        public virtual ResourceDictionary Resources { get; set; }
    }
}